﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using DotNetCore.CAP;
using EventBus.RabbitMQ;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Order.CommandHandler;
using Order.Common;
using Order.Common.Command;
using Order.Common.Dto;
namespace OrderWebApi.Controllers
{
    [Route("api/order/v1/[controller]")]
    [ApiController]
    public class OrderController : ControllerBase
    {
        private readonly IOrderService _orderService;
        private readonly ICommandService _commandService;
        private readonly IRabbitMqPublisher _rabbitMqPublisher;
        private readonly IConfiguration _configuration;

        public OrderController(IOrderService orderService, 
            ICommandService commandService, 
            IRabbitMqPublisher rabbitMqPublisher
            ,IConfiguration configuration
            )
        {
            _orderService = orderService;
            _commandService = commandService;
            _rabbitMqPublisher = rabbitMqPublisher;
            _configuration = configuration;
        }

        /// <summary>
        /// 创建订单WebApiClient
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("webapiclient")]
        public async Task<ResponseResult<string>> CreateOrderByWebApiClient([FromBody] CreateOrderDto input)
        {
            var result = await _orderService.CreateOrderByWebApiClient(input);
            return result;
        }

        /// <summary>
        /// 创建订单GRPC
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("grpc")]
        public async Task<ResponseResult<string>> CreateOrderByGrpc([FromBody] CreateOrderDto input)
        {
            var result = await _orderService.CreateOrderByGrpc(input);
            return result;
        }

        /// <summary>
        /// 创建订单事件
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("event")]
        public async Task<ResponseResult<string>> CreateOrderByEvent([FromBody] CreateOrderDto input)
        {
            var result = await _orderService.CreateOrderByEvent(input);
            return result;
        }

        TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("command")]
        public async Task<ResponseResult<string>> CreateOrder([FromBody] CreateOrderDto input)
        {
            var createOrderCommand = new CreateOrderCommand(input.CustomerId, input.SkuList);
            await _commandService.RabbitMqSendCommandAsync(createOrderCommand);
            //这里订阅 执行结果的事件，然后返回？
            return new ResponseResult<string>() { IsSucceed = true, Message = "创建成功" };
        }

        /// <summary>
        /// 订阅事件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task CommandEventResult()
        {
            tcs.SetResult("创建成功!");
            await Task.CompletedTask;
        }

        /// <summary>
        /// test1
        /// </summary>
        /// <returns></returns>
        [HttpGet("test1")]
        public async Task test1()
        {
            for (int i = 0; i < 10; i++)
            {
                var csRedisHelper = RedisConcurrentDictionary.GetRedisInstance(i.ToString());
                csRedisHelper.StringSet("test1" + i, "test1" + i);
                var c = csRedisHelper.GetString("test1" + i);
                Console.WriteLine(c);
            }
            await Task.CompletedTask;
        }

        /// <summary>
        /// apllo Test
        /// </summary>
        /// <returns></returns>
        [HttpGet("apllo")]
        public string AplloTest()
        {
            return _configuration.GetValue<string>("ConnectionService:TestApollo") + "配置name:"+ _configuration.GetValue<string>("name");
           
        }
    }
}